/*
 * @Author: hongbin
 * @Date: 2021-12-18 15:29:37
 * @LastEditors: hongbin
 * @LastEditTime: 2021-12-18 15:39:24
 * @Description:常用函数
 */

export interface IGeneratePaginationIndex {
  total: number;
  pageCount: number;
  currentPage: number;
  showItem?: number;
}

/**
 * @description: 生成分页项的索引
 * @param {*}
 * @return {number[]} 分页每一项索引
 */
export function generatePaginationIndex({
  total,
  pageCount,
  currentPage,
  showItem = 5
}: IGeneratePaginationIndex): number[] {
  const pageIndex: number[] = [];
  let pages = Math.ceil(total / pageCount); //一共能分多少页
  // 只显示5个按钮
  if (pages > showItem) {
    if (currentPage > 1) {
      if (pages - currentPage >= showItem - 1) {
        let i = -1;
        while (i < showItem - 3) {
          pageIndex.push(currentPage + i);
          i++;
        }
        pageIndex.push(pages - 1, pages);
      } else {
        let i = showItem - 1;
        while (i >= 0) {
          pageIndex.push(pages - i);
          i--;
        }
      }
    } else {
      let i = 0;
      while (i < showItem - 2) {
        pageIndex.push(currentPage + i);
        i++;
      }
      pageIndex.push(pages - 1, pages);
    }
  } else {
    while (pages > 0) {
      pageIndex.unshift(pages);
      pages--;
    }
  }
  return pageIndex;
}
